package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.fragment.MyAnswerFragment;
import com.banshuibao.bsb_android.fragment.OneOnOneFragment;

public class TabTwoActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private ImageView back;
    private RadioGroup radioGroup;
    private FrameLayout frameLayout;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private OneOnOneFragment onOneFragment;
    private MyAnswerFragment myAnswerFragment;
    private RadioButton mRadioButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_two);
        manager = getSupportFragmentManager();
        initView();
        //判断专家的悬赏是否被采纳
        String flag = getIntent().getStringExtra("flag");
        if (!TextUtils.isEmpty(flag) && flag.equals("1")) {
            mRadioButton.setChecked(true);
            showFragment(R.id.rb_tobe_two);
        } else {
            showFragment(R.id.rb_one);
        }
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back_tab);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        frameLayout = (FrameLayout) findViewById(R.id.tab_frame);
        mRadioButton = findViewById(R.id.rb_tobe_two);
        back.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_tab:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_one:
                showFragment(R.id.rb_one);
                break;
            case R.id.rb_tobe_two:
                showFragment(R.id.rb_tobe_two);
                break;
        }
    }

    private void showFragment(int id) {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (id) {
            case R.id.rb_one:
                if (onOneFragment == null) {
                    onOneFragment = new OneOnOneFragment();
                    transaction.add(R.id.tab_frame, onOneFragment, "one");
                } else {
                    transaction.show(onOneFragment);
                }
                break;
            case R.id.rb_tobe_two:
                if (myAnswerFragment == null) {
                    myAnswerFragment = new MyAnswerFragment();
                    transaction.add(R.id.tab_frame, myAnswerFragment, "myanswer");
                } else {
                    transaction.show(myAnswerFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (onOneFragment != null) {
            ft.hide(onOneFragment);
        }
        if (myAnswerFragment != null) {
            ft.hide(myAnswerFragment);
        }
    }
}
